package com.shengwei.truck.driver.view.ui.task.pounds;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import androidx.lifecycle.ViewModel;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.shengwei.truck.driver.R;
import com.shengwei.truck.driver.bean.DeleteInfo;
import com.shengwei.truck.driver.bean.ImageItem;
import com.shengwei.truck.driver.databinding.ActivityImgZoomBinding;
import com.shengwei.truck.driver.utils.IntentConstants;
import com.shengwei.truck.driver.view.ui.BaseActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoViewAttacher;


/**
 * 查看图片列表，可滑动
 */
public class ImageZoomActivity extends BaseActivity<ViewModel, ActivityImgZoomBinding> implements View.OnClickListener {
	private MyPageAdapter mImgAdapter;
	private int mCurPosition;
	private int mGridType;
	private List<ImageItem> mDataList = new ArrayList<ImageItem>();

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getLastIntent();
		initView();
	}

	@Override
	protected int getContentViewId() {
		return R.layout.activity_img_zoom;
	}

	/**
	 * 获取上一页传递过来的数据
	 */
	private void getLastIntent(){
		Intent intent = getIntent();
		Bundle b = intent.getExtras();
		if(b != null){
			mCurPosition = b.getInt(IntentConstants.EXTRA_CURRENT_IMG_POSITION);
			mGridType = b.getInt(IntentConstants.EXTRA_GRID_TYPE);
			mDataList = (List<ImageItem>) b.getSerializable(IntentConstants.EXTRA_IMAGE_LIST);
			int size = mDataList.size();
			mDataBinding.tvPageTitle.setText(mCurPosition+1+" / "+size);
		}
		return;
	}

	private void initView() {
		//修改底部操作栏背景
		mDataBinding.btPhotoExit.setOnClickListener(this);
		mDataBinding.btDelPhoto.setOnClickListener(this);
		//滑动展示图片
		mDataBinding.viewpager.setOnPageChangeListener(pageChangeListener);
		mImgAdapter = new MyPageAdapter(mDataList);
		mDataBinding.viewpager.setAdapter(mImgAdapter);
		mDataBinding.viewpager.setCurrentItem(mCurPosition);
	}

	/***
	 * 删除所有图片
	 */
	private void removeImgs() {
		mDataList.clear();
		//position = 100代表删除所有，选择图片页面肯定到不了100张
		DeleteInfo deleteInfo = new DeleteInfo(0,mGridType);
		EventBus.getDefault().post(deleteInfo);
	}


	/**
	 * 删除当前正在查看的图片索引，从0开始
	 * @param curPosition
	 */
	private void removeImg(int curPosition) {
		if (curPosition + 1 <= mDataList.size()) {
			mDataList.remove(curPosition);
			DeleteInfo deleteInfo = new DeleteInfo(curPosition,mGridType);
			EventBus.getDefault().post(deleteInfo);
		}
	}

	public void showPageTitle(int curPosition){
		int size  = mDataList.size();
		if(0 <= curPosition && curPosition < size) {
			mDataBinding.tvPageTitle.setText(curPosition+1 + " / "+size);
		}

	}

	/**
	 * 滑动一下锁定一下当前position
	 */
	private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
		public void onPageSelected(int position) {
			mCurPosition = position;
			showPageTitle(mCurPosition);
		}
		public void onPageScrolled(int position, float offset, int offsetPixels) {
		}
		public void onPageScrollStateChanged(int state) {
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btPhotoExit:
				finish();
				break;
			case R.id.btDelPhoto:
				delPhoto();
				break;
		}
	}

	/**
	 * 删除照片
	 */
	public void delPhoto(){
		if (mDataList.size() == 1) {
			removeImgs();
			finish();
		}
		else {
			removeImg(mCurPosition);
			mDataBinding.viewpager.removeAllViews();
			mImgAdapter.removeView(mCurPosition);
			mImgAdapter.notifyDataSetChanged();
			showPageTitle(mCurPosition);
		}
	}


	class MyPageAdapter extends PagerAdapter
	{
		private List<ImageItem> dataList = new ArrayList<ImageItem>();
		private ArrayList<ImageView> mViews = new ArrayList<ImageView>();

		public MyPageAdapter(List<ImageItem> dataList)
		{
			this.dataList = dataList;
			int size = dataList.size();
			for (int i = 0; i != size; i++) {
				ImageView iv = new ImageView(ImageZoomActivity.this);
				showPhoto(iv,dataList.get(i).sourcePath);
				//ImageDisplayer.getInstance(ImageZoomActivity.this).displayBmp(iv, null, dataList.get(i).sourcePath, false);
				iv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
				mViews.add(iv);
			}
		}

		private void showPhoto(ImageView imvPhoto,String filePath) {
			PhotoViewAttacher attacher = new PhotoViewAttacher(imvPhoto);
			attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
				@Override
				public void onPhotoTap(View view, float x, float y) {
					ImageZoomActivity.this.finish();
				}
			});

			//加载原图optionRequestOptions options = new RequestOptions().override(Target.SIZE_ORIGINAL);
			Glide.with(ImageZoomActivity.this)
					.load(filePath)
					.into(imvPhoto);
		}

		public int getItemPosition(Object object)
		{
			return POSITION_NONE;
		}

		public Object instantiateItem(View container, int position) {
			ImageView iv = mViews.get(position);
			((ViewPager) container).addView(iv);
			return iv;
		}

		public void destroyItem(View container, int position, Object arg2)
		{
			if (mViews.size() >= position + 1) {
				((ViewPager) container).removeView(mViews.get(position));
			}
		}

		public boolean isViewFromObject(View view, Object arg1) {
			return view == arg1;
		}

		@Override
		public int getCount() {
			return dataList.size();
		}

		public void removeView(int position) {
			if (position + 1 <= mViews.size()) {
				mViews.remove(position);
			}
		}

	}
}